package com.example.tkserver.service.impl;

import com.example.tkserver.dao.QuestionDao;
import com.example.tkserver.entity.Answer;
import com.example.tkserver.entity.Question;
import com.example.tkserver.dao.RedisCacheDao;
import com.example.tkserver.service.interfaces.QuestionService;
import com.example.tkserver.entity.MyException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log4j2
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionDao questionDao;

    @Autowired
    RedisCacheDao redisCacheDao;
    @Override
    public Question getQuestion(int qid) {
        log.info(""+qid);
        if (qid == -1){
            log.info("获取随机提问");
            //随机获取一个提问
            Question q = questionDao.randomQuestion();
            if (q == null){
                throw new MyException("服务器发生未知异常...");
            }
            log.info("获取随机提问完成:"+q.toString());
            return q;
        }else {
            log.info("查询提问:"+qid);
            Question q = questionDao.getQuestion(qid);
            if (q == null){
                throw new MyException("服务器发生未知异常...");
            }
            log.info("获取随机提问完成:"+q.toString());
            return q;
        }
    }

    @Override
    public int getQuestionNum() {
        log.info("查询问题数量");
        int num = questionDao.getQuestionsNum();
        log.info("查询问题数量完成:"+num);
        return num;
    }


    @Override
    public List<Question> getNewQuestionList(int start,int end){
        log.info("查询最新问题:"+start+"-"+end);
        List<Question> result = questionDao.getNewQuestionsBetween(start,end);
        log.info("查询最新问题完成:"+result);
        return result;
    }

    @Override
    public int getHotQuestionNum() {
        log.info("查询热门问题数量");
        // 从redis查询满足热门的最小评论数
        Integer hotAnswerNum = redisCacheDao.getObject("hotMinAnswerNum",Integer.class);
        log.info("获取最小数量:"+hotAnswerNum);
        // 如果没有这个数据 则添加默认0
        if (hotAnswerNum == null){
            redisCacheDao.add("hotMinAnswerNum",0);
            hotAnswerNum = 0;
        }
        // 查询
        int num = questionDao.getHotQuestionsNum(hotAnswerNum);
        log.info("查询热门问题数量完成:"+num);
        return num;
    }

    @Override
    public List<Question> getHotQuestionList(int start, int end) {
        log.info("查询热门问题:"+start+"-"+end);
        Integer hotAnswerNum = redisCacheDao.getObject("hotMinAnswerNum",Integer.class);
        log.info("获取最小数量:"+hotAnswerNum);
        // 如果没有这个数据 则添加默认0
        if (hotAnswerNum == null){
            redisCacheDao.add("hotMinAnswerNum",0);
            hotAnswerNum = 0;
        }
        List<Question> result = questionDao.getHotQuestionsBetween(hotAnswerNum,start,end);
        log.info("查询热门问题完成:"+result);
        return result;
    }

    @Override
    public int getWaitQuestionNum() {
        log.info("查询待答数量");
        int num = questionDao.getWaitQuestionNum();
        log.info("查询待答数量完成:"+num);
        return num;
    }

    @Override
    public List<Question> getWaitQuestionList(int start, int end) {
        log.info("查询待答问题:"+start+"-"+end);
        List<Question> result = questionDao.getWaitQuestionsBetween(start,end);
        log.info("查询待答问题完成:"+result);
        return result;
    }

    @Override
    public Answer publishQuestion(Question question) {
        log.info("发布一个新的问题:"+question);
        question.setDatetime(System.currentTimeMillis()/1000);
        if (questionDao.pushQuestion(question) == -1){
            throw new MyException("发布问题失败，请联系管理员进行反馈！");
        }
        log.info("发布一个新的问题完成");
        return null;
    }
}
